package com.business.service.oa;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.business.bean.oa.CalendarBean;
import com.business.bean.oa.ReportDay;
import com.business.bean.system.UserBean;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.DateFormat;

/**
 * 工作日历生成服务
 */
public class CalendarService {

	/**
	 * 给指定用户创建指定月份的日报数据
	 * 
	 * @param userID - 用户编号
	 * @param date   - 日期
	 */
	public PageBean createEmployeeCalendar(Service service, String userID, Date date) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(userID);
		UserBean user = dao.get();
		String departID = user.getDepartID();
		List<CalendarBean> cs = getCalendarBeanList(dao, query, departID, date);
		if (cs.size() == 0) {
			res.setMessage("没有找到此用户所属的部门，无法创建他的工作日报！");
			return res;
		}
		List<ReportDay> list = new ArrayList<>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		for (CalendarBean c : cs) {
			String day = c.getCalendarDate();
			ReportDay w = new ReportDay();
			w.setUserID(user.getUserID());
			w.setDepartID(departID);
			w.setCalendarDate(day);
			w.setDayType(c.getDayType());
			calendar.setTime(DateFormat.format(day + " 00:00:00", DateFormat.DATE_FORMAT_3));
			calendar.set(Calendar.HOUR, c.getStartWorkHour());
			calendar.set(Calendar.MINUTE, c.getStartWorkMinute());
			w.setStartWorkTime(calendar.getTime());
			calendar.set(Calendar.HOUR, c.getEndWorkHour());
			calendar.set(Calendar.MINUTE, c.getEndWorkMinute());
			w.setEndWorkTime(calendar.getTime());
			list.add(w);
		}
		query.createBatchInsertSql(list);
		dao.batchUpdate();
		return res;
	}

	/**
	 * 根据部门编号从日历表中查出从当天起的日历数据列表
	 * 
	 * @param departID
	 * @param date
	 * @return
	 */
	private List<CalendarBean> getCalendarBeanList(BeanDAO dao, BeanSQL query, String departID, Date date) {
		// 获取下个月的第一天
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		String sd = DateFormat.format(date, DateFormat.DATE_FORMAT_1);
		String ed = DateFormat.format(calendar.getTime(), DateFormat.DATE_FORMAT_1);
		query.setEntityClass(CalendarBean.class);
		query.setSQL("select * from OA_CALENDAR where departID=? and calendarDate>=? and calendarDate<?");
		query.addParameter(departID);
		query.addParameter(sd);
		query.addParameter(ed);
		List<CalendarBean> cs = dao.list();
		while (cs.size() == 0 && departID != null && departID.length() > 4) {
			departID = departID.substring(0, departID.length() - 4);
			query.clearParameterList();
			query.addParameter(departID);
			query.addParameter(sd);
			query.addParameter(ed);
			cs = dao.list();
		}
		return cs;
	}

	/**
	 * 获取指定时间段的日历
	 * @param dao
	 * @param query
	 * @param departID
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public List<CalendarBean> getCalendarBeanList(BeanDAO dao, BeanSQL query, String departID, String sDate, String eDate) {
		query.setEntityClass(CalendarBean.class);
		query.setSQL("select * from OA_CALENDAR where departID=? and calendarDate>=? and calendarDate<?");
		query.addParameter(departID);
		query.addParameter(sDate);
		query.addParameter(eDate);
		List<CalendarBean> cs = dao.list();
		while (cs.size() == 0 && departID != null && departID.length() > 4) {
			departID = departID.substring(0, departID.length() - 4);
			query.clearParameterList();
			query.addParameter(departID);
			query.addParameter(sDate);
			query.addParameter(eDate);
			cs = dao.list();
		}
		return cs;
	}
}